/**
 * Created by jiachenpan on 16/11/18.
 */

export function isvalidUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}
export function checkIdCard (rule, value, callback){
  if (value && value.length!==18) {
    return callback(new Error('身份证号码必须为18位'));
  }else{
    return callback();
  }
}


let reg = {
  required:function(message) {
    return { required: true, message: message || '输入不能为空',trigger:'blur'}
  },
  number:function(){
    return { type: 'number', message: '输入只能为数字',trigger:'blur'}
  },
  limitLength:function(obj) {
    let rule = {
      min: obj.min || 2,
      max: obj.max || 30,
      trigger: obj.trigger || 'blur'
    }
    if(rule.max == rule.min){
      rule.message = `长度只能为${rule.min}个字符`
    }else
      rule.message = `长度为 ${rule.min} 到 ${rule.max} 个字符`
    return rule
  },
  limitDigits:function(){
    return {message:'输入数字必须是保留小数点后两位',trigger: 'blur',pattern:/^(([1-9]{1}\d*)|(0{1}))(\.\d{2})$/}
  },
  lgZero:function(){
    return {message:'缴费金额必须大于零',trigger: 'blur',pattern:/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/}
  }
}
export default reg
